package com.yjd.bi.fund.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.bi.fund.model.PfBalanceModel;
import com.yjd.bi.fund.service.IPfBalanceService;
import com.yjd.bi.fund.dao.IPfBalanceDao;
import sun.security.provider.Sun;

/**
 * <b>description</b>：提现记录业务实现<br>
 * <b>time</b>：2018-09-10 10:51:03 <br>
 * <b>author</b>：  weili valie.lin@foxmail.com
 */
@Service("pfBalanceService")
public class PfBalanceServiceImpl extends BaseServiceImpl implements IPfBalanceService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public PfBalanceModel insert(PfBalanceModel model) throws Exception {
		return this.pfBalanceDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(PfBalanceModel model) throws Exception {
		return this.pfBalanceDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.pfBalanceDao.deleteModel(paramMap, true);
	}

	@Resource
	private IPfBalanceDao pfBalanceDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.pfBalanceDao;
	}
	/**
	 * 包含借款人的余额
	 *
	 * @param cur_date
	 * @return
	 */
	@Override
	public double getfundsSumAllAvailable(Long cur_date) throws Exception {
		BigDecimal sum = BigDecimal.ZERO;
		PfBalanceModel model = pfBalanceDao.getModelOne(FrameUtil.newHashMap("run_date",cur_date),false);
		if (model != null){
			sum = model.getInvest_balance().add(model.getBankdepo_invest_balance())
					.add(model.getBorrow_balance()).add(model.getBankdepo_borrow_balance());
		}
		return sum.doubleValue();
	}

	/**
	 * 不包含借款人余额
	 *
	 * @param cur_date
	 * @return
	 */
	@Override
	public double getfundsSumAvailable(Long cur_date) throws Exception {
		BigDecimal sum = BigDecimal.ZERO;
		PfBalanceModel model = pfBalanceDao.getModelOne(FrameUtil.newHashMap("run_date",cur_date),false);
		if (model != null){
			sum = model.getInvest_balance().add(model.getBankdepo_invest_balance());
		}
		return sum.doubleValue();
	}
}
